import Vue from 'vue'
import App from './App'
import http from '@/utils/request'
import { showMessage } from '@/utils/message'
import store from './store'

// 1. 挂载http对象到全局
Vue.prototype.$http = http
// 2. 挂在消息提示到全局
Vue.prototype.$msg = showMessage

// 2. 创建全局过滤器
Vue.filter('formatPrice', (value) => {
  return '¥' + Number(value).toFixed(2)
})

App.mpType = 'app'

function isPromise(obj) {
  return (
    !!obj &&
    (typeof obj === "object" || typeof obj === "function") &&
    typeof obj.then === "function"
  );
}

uni.addInterceptor({
  returnValue(res) {
    if (!isPromise(res)) {
      return res;
    }
    return new Promise((resolve, reject) => {
      res.then((res) => {
        if (res[0]) {
          reject(res[0]);
        } else {
          resolve(res[1]);
        }
      });
    });
  },
});

const app = new Vue({
  ...App,
  store
})
app.$mount()
